package com.tmc.service.intcar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.intcar.EthCancelOrderReq;
import com.tmc.bean.intcar.EthCancelOrderResp;
import com.tmc.entity.intcar.IntcarOrder;
import com.tmc.vendor.intcar.common.ServiceCenter;
import com.tmc.vendor.intcar.common.VendorService;

/**
 * @author zhang
 * @date 2017年12月21日 下午5:20:40 
 */
@Service
public class IntcarCancelOrderServiceImpl {

	private Log log = LogFactory.getLog(this.getClass());
	
	
	@Autowired
	private IntcarOrderServiceImpl orderService;
	
	@Transactional
	public CommonResult canceling(ContextInfo context, EthCancelOrderReq request){
		IntcarOrder dbOrder = orderService.getById(context, request.getOrderID());
		if(dbOrder == null){
			CommonResult.makeFail("ID为"+request.getOrderID()+"的订单不存在！");
		}
		VendorService service = ServiceCenter.getService(dbOrder.getVendor());
		try {
			//先取消供应商的订单
			request.setVendorOrderID(dbOrder.getVendorOrderID());
			EthCancelOrderResp resp = service.cancelOrder(context, request);
			if(resp.isSuccess()){
				//取消本地订单
				orderService.canceled(context, dbOrder);
				return CommonResult.makeSuccess("订单取消成功！");
			}
			return CommonResult.makeFail("取消失败！");
		} catch (Exception e) {
			log.error("取消订单出错！", e);
		}
		return CommonResult.makeFail("取消订单出错！");
	}

}
